package com.iwhalecloud.bss.kite.cucc.service.job.scene;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import org.springframework.util.StopWatch;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.executor.enums.ExecutorTypeEmun;
import com.iwhalecloud.bss.kite.common.executor.task.ExecuteTask;
import com.iwhalecloud.bss.kite.common.executor.vo.ExecuteResult;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.scene.IInstLoader;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosDTO;
import com.iwhalecloud.bss.kite.manager.transaction.KiteTransactional;
import com.iwhalecloud.bss.kite.manager.util.KiteTransactionUtils;
import com.ztesoft.bss.common.bo.LoginInfo;
import com.ztesoft.common.logger.api.ZLogger;
import com.ztesoft.common.logger.factory.ZLoggerFactory;

/**
 * 合同、付费关系、折扣等信息还原线程类
 * @author jojo
 *
 */
public class OfferInstRelInfoReloadExecuteTask extends ExecuteTask {

    private static final ZLogger LOGGER = ZLoggerFactory.getLogger(OfferInstRelInfoReloadExecuteTask.class, KeyConsts.LOG_MODULE);

    private LoginInfo loginInfo;
    private String sceneInstId;
    private String custId;
    private String staffId;
    private String operType;
    private String transactionId;
    private KitePosDTO kitePosDTO;
    private IInstLoader loader;

    public OfferInstRelInfoReloadExecuteTask() {

    }

    public OfferInstRelInfoReloadExecuteTask(ExecutorTypeEmun typeEmun, String taskId, String sceneInstId, String operType, String custId, String staffId, KitePosDTO kitePosDTO, LoginInfo loginInfo, IInstLoader loader, String transactionId) {
        super.setTypeEmun(typeEmun);
        super.setTaskId(taskId);
        this.sceneInstId = sceneInstId;
        this.custId = custId;
        this.staffId = staffId;
        this.operType = operType;
        this.loginInfo = loginInfo;
        this.transactionId = transactionId;
        this.kitePosDTO = kitePosDTO;
        this.loader = loader;
    }

    @Override
    @KiteTransactional
    public ExecuteResult<String> call() throws Exception {
        KiteTransactionUtils.setTransactionId(transactionId);
        ExecuteResult<String> result = new ExecuteResult<String>();
        long time1 = System.currentTimeMillis();
        // 礼包处理
        try {
            StopWatch sw = new StopWatch("变更场景初始化-还原属性值-还原合同、付费关系线程");
            sw.start(loader.toString());

            loader.loadInst(sceneInstId, operType, custId, staffId, kitePosDTO, loginInfo);

            sw.stop();
            LOGGER.info(sw.prettyPrint());
        }
        catch(Exception e) {
            LOGGER.error(ErrorConsts.SYSTEM_ERROR.getErrorCode(), e.getCause(),e.getMessage());
            ErrorConsts.INF_SALES_HANDLE_EXCEPTION.throwOut(e.getMessage());
        }
        finally {
            //KiteTransactionUtils.commit();
            //KiteTransactionUtils.remove();
        }
        long time2 = System.currentTimeMillis();
        result.setResult(KiteStringUtils.valueOf(time2 - time1));
        return result;

    }

}
